<?php
namespace app\console\elasticsearch;
use Elasticsearch\Client;
use request\request;

class ElasticsearchController
{
    /**
     * 创建索引
     * @return array
     */
    public function index(Client $client): array
    {
        //创建索引
        return $client->indices()->create([
            'index' => 'my_index'
        ]);
    }

    /**
     * 创建索引
     * @param Client $client
     * @return array
     */
    public function search(Client $client, request $request)
    {
        //创建索引
        $res = $client->search([
            'index' => 'brand',
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            ['match' => ['name' => $request->get('name')]],
                        ]
                    ]
                ]
            ]
        ]);
        return [
            'data' => $res,
            'runtime' => runtime()
        ];
    }
}